Submitting Trades





Kerry Back

Predictions

  • Retrain your model/pipeline using all months in the CloudClusters database and save it using joblib.dump. You only need to do this once.
  • To make Alpaca trades, either initial trades or updates:
    • Get the latest feature values from the Rice database in a dataframe indexed by ticker
    • Load your model/pipeline and use it with the features to make predictions
df["predict"] = pipe.predict(df[features])

Tradable and shortable

from alpaca.trading.client import TradingClient

trading_client = TradingClient(KEY, SECRET_KEY, paper=True)
assets = trading_client.get_all_assets()

df["tradable"] = {
  x.symbol: x.trabable for x in assets
}

df["shortable"] = {
  x.symbol: x.shortable for x in assets
}

Quotes

from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockLatestQuoteRequest

data_client = StockHistoricalDataClient(KEY, SECRET_KEY)
params = StockLatestQuoteRequest(
  symbol_or_symbols=df.index.to_list()
)
quotes = data_client.get_stock_latest_quote(params)

df["ask"] = {x: quotes[x].ask_price for x in quotes}
df["bid"] = {x: quotes[x].bid_price for x in quotes}

Account equity and current positions


account = trading_client.get_account()
equity = float(account.equity)

Current positions

positions = trading_client.get_all_positions() if len(positions) > 0: positions = {x.symbol: int(x.qty) for x in positions} positions = pd.Series(positions) df[“current”] = positions df[“current”] = df.current.fillna(0) else: df[“current”] = 0

Example

  • 150/50 portfolio
  • Longs = top 100 equally weighted
  • Shorts = bottom 100 equally weighted

Equal weight top 100 longs and equal weight bot